package net.jumpengine.bean;

import java.util.List;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import net.jumpengine.Manager;
import net.jumpengine.util.UDraw;

/**
 * 简单实现的文字Base,如果width大于0,则会以width分割字符串为多个,多行显示
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年2月24日 下午4:53:48<br />
 * @date
 */
public class TextBase extends Base {
	protected String text;
	protected Color color=Color.WHITE;
	protected Color backColor;
	protected Font font=Manager.getFont();
	protected int rowSpacing=3;
	protected List<String> list;
	protected boolean change=true;
	
	public TextBase(int x, int y,String text) {
		this(x, y, 0,text);
	}

	public TextBase(int x, int y, int width,String text) {
		super(x, y, width, 0);
		this.text=text;
	}
	
	public TextBase(int x, int y, int width,String text, int rowSpacing,Font font) {
		super(x, y, width, 0,0);
		this.text=text;
		this.rowSpacing=rowSpacing;
		this.font=font;
	}

	@Override
	public void draw(GraphicsContext g) {
		if(!visible)return;
		if(color!=null)g.setFill(color);
		if(font!=null)g.setFont(font);
		if(width==0) {
			UDraw.drawString(g, text, x, y);
		}else {
			if(list==null || change) {
				list=UDraw.splitString(text, width, font);
				change=false;
			}
			UDraw.drawStrings(g, list, x, y, rowSpacing);
		}
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		if(width==0)return;
		int w=UDraw.stringWidth(text, font);
		if(width<w) {
			x-=(w+rowSpacing*2-width)/2;
			width=w+rowSpacing*2;
		}
		change=true;
	}
	
	@Override
	public void setWidth(int width) {
		super.setWidth(width);
		change=true;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Color getBackColor() {
		return backColor;
	}

	public void setBackColor(Color backColor) {
		this.backColor = backColor;
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	/** 获取行间距 */
	public int getRowSpacing() {
		return rowSpacing;
	}

	/** 设置行间距,默认为3像素 */
	public void setRowSpacing(int rowSpacing) {
		this.rowSpacing = rowSpacing;
	}

}
